home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Parser / listnode.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  1.0 KB  |  71 lines

  1. /* List a node on a file */
  2.  
  3. #include "pgenheaders.h"
  4. #include "token.h"
  5. #include "node.h"
  6.  
  7. /* Forward */
  8. static void list1node Py_PROTO((FILE *, node *));
  9. static void listnode Py_PROTO((FILE *, node *));
  10.  
  11. void
  12. PyNode_ListTree(n)
  13.     node *n;
  14. {
  15.     listnode(stdout, n);
  16. }
  17.  
  18. static int level, atbol;
  19.  
  20. static void
  21. listnode(fp, n)
  22.     FILE *fp;
  23.     node *n;
  24. {
  25.     level = 0;
  26.     atbol = 1;
  27.     list1node(fp, n);
  28. }
  29.  
  30. static void
  31. list1node(fp, n)
  32.     FILE *fp;
  33.     node *n;
  34. {
  35.     if (n == 0)
  36.         return;
  37.     if (ISNONTERMINAL(TYPE(n))) {
  38.         int i;
  39.         for (i = 0; i < NCH(n); i++)
  40.             list1node(fp, CHILD(n, i));
  41.     }
  42.     else if (ISTERMINAL(TYPE(n))) {
  43.         switch (TYPE(n)) {
  44.         case INDENT:
  45.             ++level;
  46.             break;
  47.         case DEDENT:
  48.             --level;
  49.             break;
  50.         default:
  51.             if (atbol) {
  52.                 int i;
  53.                 for (i = 0; i < level; ++i)
  54.                     fprintf(fp, "\t");
  55.                 atbol = 0;
  56.             }
  57.             if (TYPE(n) == NEWLINE) {
  58.                 if (STR(n) != NULL)
  59.                     fprintf(fp, "%s", STR(n));
  60.                 fprintf(fp, "\n");
  61.                 atbol = 1;
  62.             }
  63.             else
  64.                 fprintf(fp, "%s ", STR(n));
  65.             break;
  66.         }
  67.     }
  68.     else
  69.         fprintf(fp, "? ");
  70. }
  71.